home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / faq / comp / forthfaq / case_end < prev    next >
Internet Message Format  |  1993-12-14  |  8KB

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!nic.hookup.net!news.kei.com!sol.ctr.columbia.edu!howland.reston.ans.net!gatech!pitt!willett!ForthFAQ
  2. From: ForthFAQ@willett.pgh.pa.us (FAQ account for comp.lang.forth)
  3. Newsgroups: comp.lang.forth,comp.answers,news.answers
  4. Subject: Forth FAQ: CASE,OF,ENDOF,ENDCASE.  (l/m 07.Nov.93)
  5. Message-ID: <4828.UUL1.3#5129@willett.pgh.pa.us>
  6. Date: 15 Dec 93 01:39:59 GMT
  7. Expires: Wed, 22 Dec 93 23:59:59 EDT
  8. References: <4819.UUL1.3#5129@willett.pgh.pa.us>
  9. Followup-To: poster
  10. Lines: 218
  11. Approved: news-answers-request@MIT.Edu
  12. Xref: senator-bedfellow.mit.edu comp.lang.forth:14693 comp.answers:3024 news.answers:15821
  13.  
  14. Archive-name: ForthFaq/CASE_ENDCASE
  15. Last-modified: 07.Nov.93
  16. Version: 1.2
  17.  
  18.  
  19.  
  20.   These are messages that I thought deserved to be preserved.  -dwp
  21.  
  22.  
  23.   Recent changes:
  24.   1993-11-07 dwp     Started change log.  Added code from Randolph Peters,
  25.                      and comments by john - Rible.
  26.  
  27.  
  28.  -----------------------
  29.  
  30.     From: eaker@ukulele.crd.ge.com (Chuck Eaker)
  31.     Subject: Re: Wanted .. CASE,OF,ENDOF,ENDCASE
  32.     Message-ID: <1992Nov25.164255.23225@crd.ge.com>
  33.     Date: 25 Nov 92 16:42:55 GMT
  34.  
  35.     In article <jax.722669998@well.sf.ca.us>,
  36.         jax@well.sf.ca.us (Jack J. Woehr) writes:
  37.     |> In <1992Nov24.101857.84@wronz.org.nz> mentink@wronz.org.nz writes:
  38.     |> 
  39.     |> 
  40.     |> 
  41.     |> >    Can anyone help with source ( masm/forth) to the CASE statement
  42.     |> >    word set. I.E .... CASE OF ENDOF ENDCASE ....
  43.     |> 
  44.     |>     Baden's CASE is in FORTH Dimensions VIII/5.
  45.     |> 
  46.     |>     Eaker, who wrote and enduring CASE construct, checks into
  47.     |> this newsgroup once and a while. Charles?
  48.     |> 
  49.     |>         =jax=
  50.     |> -- 
  51.     |>  # jax@well.{UUCP,sf.ca.us}  # #  Member  # # Vice President,       #
  52.     |>  # du!isis!koscej!jax        # # X3J14 TC # #  Forth Interest Group #
  53.     |>  # JAX on GEnie              # # for ANS  # #   P.O. Box 8231       #
  54.     |>  # SYSOP RCFB (303) 278-0364 # #  Forth   # #    San Jose CA 95155  #
  55.  
  56.     1. FIG-Forth
  57.     Here is the source for FIG-Forth published with the original article
  58.     (Forth Dimensions, Vol. II, No. 3, pp. 37-40.). The ?PAIRS word was
  59.     FIG-Forth's way of implementing a small amount of syntax checking.
  60.  
  61.      : CASE     ?COMP CSP @ !CSP 4 ; IMMEDIATE
  62.      : OF       4 ?PAIRS COMPILE OVER COMPILE = COMPILE OBRANCH
  63.             HERE 0 ,   COMPILE DROP  5 ; IMMEDIATE
  64.      : ENDOF    5 ?PAIRS COMPILE BRANCH HERE 0 ,
  65.             SWAP 2 [COMPILE] ENDIF 4 ; IMMEDIATE
  66.      : ENDCASE  4 ?PAIRS COMPILE DROP
  67.             BEGIN SP@ CSP @ = 0=
  68.               WHILE 2 [COMPILE ENDIF REPEAT
  69.             CSP ! ; IMMEDIATE
  70.  
  71.     1a. Here is additional source for FIG-Forth published in Forth
  72.     Dimensions, Vol. III, No. 6, pp. 187-188 in an article by Alfred J.
  73.     Monroe.  He adds a primitive compiled by OF which reduces the amount
  74.     of code compiled by OF. Use the definitions of CASE, ENDOF, and
  75.     ENDCASE given above.
  76.  
  77.      : (OF)     OVER = IF DROP 1 ELSE 0 ENDIF ;
  78.      : OF       4 ?PAIRS COMPILE (OF) COMPILE 0BRANCH
  79.             HERE 0 , 5 ; IMMEDIATE
  80.  
  81.     Mr. Monroe also gave code for some interesting variants:
  82.  
  83.      : (<OF)    OVER > IF DROP 1 ELSE 0 ENDIF ;
  84.      : <OF      4 ?PAIRS COMPILE (<OF) COMPILE 0BRANCH
  85.             HERE 0 , 5 ; IMMEDIATE
  86.      : (>OF)    OVER > IF DROP 1 ELSE 0 ENDIF ;
  87.      : >OF      4 ?PAIRS COMPILE (>OF) COMPILE 0BRANCH
  88.             HERE 0 , 5 ; IMMEDIATE
  89.      : RANGE    >R OVER DUP R> 1+ < IF SWAP 1- > IF DROP 1 ELSE 0
  90.             ENDIF ELSE DROP DROP 0 ENDIF ;
  91.      : RNG-OF   4 ?PAIRS COMPILE RANGE COMPILE 0BRANCH
  92.             HERE 0 , 5 ; IMMEDIATE
  93.  
  94.     1b. It is quite common to define (OF) as a CODE word and have
  95.     it combine the functions of the run-time (OF) and the compile-time
  96.     0BRANCH in the previous definitions. This reduces the amount of
  97.     compiled code even more.
  98.      CODE (OF) ( 1. Remove the top element of the stack and call it A.
  99.              2. If A equals the new top element of the stack,
  100.                 remove the new top element of the stack,
  101.                 skip over the branch vector, and execute
  102.                 the code which follows it.
  103.             Else
  104.                 continue execution at the location indicated
  105.                 by the branch vector.
  106.             ) END-CODE
  107.      : OF      4 ?PAIRS COMPILE (OF) HERE 0 , 5 ; IMMEDIATE
  108.  
  109.     2. dpANS-3
  110.     dpANS-3 contains the following definitions (p. 133) to illustrate
  111.     control structure extension. Note that it would be quite easy to
  112.     optimize OF along the lines suggested above. Note also that there is no
  113.     syntax checking.  These words may appear anywhere and not necessarily
  114.     combined with each other. In fact, ENDOF may be dispensed with entirely
  115.     and replaced with ELSE. Compile-time monitoring of the syntax of
  116.     control structure words is a perennial Forth problem.
  117.  
  118.      0 CONSTANT CASE IMMEDIATE  ( init count of OFs )
  119.  
  120.      : OF  ( #of -- orig #of+1 / x -- )
  121.         1+    ( count OFs )
  122.         >R    ( move off the stack in case the control-flow )
  123.           ( stack is the data stack. )
  124.         POSTPONE OVER  POSTPONE = ( copy and test case value )
  125.         POSTPONE IF    ( add orig to control flow stack )
  126.         POSTPONE DROP  ( discards case value if = )
  127.         R> ;           ( we can bring count back now )
  128.      IMMEDIATE
  129.  
  130.      : ENDOF  ( orig1 #of -- orig2 #of )
  131.         >R    ( move off the stack in case the control-flow )
  132.           ( stack is the data stack. )
  133.         POSTPONE ELSE
  134.         R> ;  ( we can bring count back now )
  135.      IMMEDIATE
  136.  
  137.      : ENDCASE ( orig 1..orign #of -- )
  138.         POSTPONE DROP  ( discard case value )
  139.         0 ?DO
  140.           POSTPONE THEN
  141.         LOOP ;
  142.      IMMEDIATE
  143.  
  144.     -- 
  145.     Chuck Eaker / P.O. Box 8, K-1 3C12 / Schenectady, NY 12301 USA
  146.     eaker@crd.ge.com        eaker@crdgw1.UUCP       (518) 387-5964
  147.  
  148.  
  149.  
  150.  -----------------------
  151.  
  152.     From: sorry no e-mail address (Randolph Peters)
  153.     Subject: pocket forth: case...endcase
  154.     Message-ID: <sorry?no?e-mail?address-221093104409@meded6.med.upenn.edu>
  155.     Date: 22 Oct 93 14:49:04 GMT
  156.  
  157.     What follows is code I have cobbled together to create a 
  158.     case construct in pocket forth. I have found several instances
  159.     where it would be useful to have a multi-branching if, and so
  160.     I made this one up. Since I haven't seen the FAQ on case 
  161.     endcase recently, I am providing this for the benefit of the
  162.     pocket forthers out there.
  163.  
  164.  
  165.     : case      0 ; immediate
  166.     : of
  167.       [ ' over literal ] compile
  168.       [ ' =    literal ] compile
  169.       [compile] if ; immediate  
  170.     : endof     [compile] else ; immediate
  171.     : otherwise ; immediate ( syntactic sugar)
  172.     : endcase   
  173.       begin  ?dup while [compile] then  repeat 
  174.       [ ' drop literal ] compile  ; immediate
  175.  
  176.  
  177.     ( example of its use )
  178.  
  179.     123 constant SPADE
  180.     234 constant HEART
  181.     345 constant DIAMOND
  182.     456 constant CLUB
  183.  
  184.     : ?cardtype ( n -- )
  185.       case 
  186.        spade of ." It's a spade" cr endof
  187.        heart of ." It's a heart" cr endof
  188.        diamond of ." It's a diamond" cr endof
  189.        club of ." It's a club" cr endof
  190.        otherwise ." I don't recognize this suit." 
  191.       endcase ;
  192.  
  193.     234 ?cardtype
  194.  
  195.     I found this useful in several cases, so please, no flames about
  196.     the purity or appropriateness of case...endcase in forth. 
  197.  
  198.  
  199.     Randolph M. Peters
  200.  
  201.     Standard disclaimer.
  202.  
  203.  
  204.  
  205.  -----------------------
  206.  
  207.     From: jrible@cup.portal.com (john - Rible)
  208.     Subject: Re: pocket forth: case...endcase
  209.     Message-ID: <93973@cup.portal.com>
  210.     Date: Sat, 23 Oct 93 07:16:05 PDT
  211.  
  212.     Randolph Peters submitted some code for a CASE ...  ENDCASE
  213.     structure that is significantly different from most of the
  214.     implementations that I know of.  I apologize for not copying and
  215.     changing the code directly, but Portal's online mailer is crude
  216.     when used with just Windows terminal program.
  217.  
  218.     Most implementations do a drop AFTER the 'if' in the definition of
  219.     OF, removing the case variable from the stack.  Then ENDCASE has
  220.     its drop BEFORE it resolves the forward references.  Both methods
  221.     work, but the user better know which style is implemented in
  222.     his/her system.
  223.  
  224.     -John
  225. ---
  226. If you have any questions about ForthNet/comp.lang.forth or any information
  227. to add/delete or correct in this message or any suggestions on formatting or
  228. presentation, please contact Doug Philips at one of the following addresses:
  229.           Internet: dwp@willett.pgh.pa.us
  230.           Usenet:   ...!uunet!willett.pgh.pa.us!dwp
  231.           GEnie:    D.PHILIPS3
  232.